﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.ComponentModel;
using libConn;

namespace ApartmentSale
{
    public class ContractView : INotifyPropertyChanged, ICloneable
    {
        private int _contract_id;
        public int contract_id
        {
            get
            {
                return _contract_id;
            }
            set
            {
                _contract_id = value;
                NotifyPropertyChanged("contract_id");
            }
        }

        private string _contract_code;
        public string contract_code
        {
            get
            {
                return _contract_code;
            }
            set
            {
                _contract_code = value;
                NotifyPropertyChanged("contract_code");
            }
        }

        private int _contract_status;
        public int contract_status
        {
            get
            {
                return _contract_status;
            }
            set
            {
                _contract_status = value;
                NotifyPropertyChanged("contract_status");
            }
        }

        private double _contract_debate_rate;
        public double contract_debate_rate 
        {
            get
            {
                return _contract_debate_rate;
            }
            set
            {
                _contract_debate_rate = value;
                NotifyPropertyChanged("contract_debate_rate");
            }
        }

        private int _contract_payment_terms;
        public int contract_payment_terms
        {
            get
            {
                return _contract_payment_terms;
            }
            set
            {
                _contract_payment_terms = value;
                NotifyPropertyChanged("contract_payment_terms");
            }
        }

        private double _contract_payment_debate_rate;
        public double contract_payment_debate_rate
        {
            get
            {
                return _contract_payment_debate_rate;
            }
            set
            {
                _contract_payment_debate_rate = value;
                NotifyPropertyChanged("contract_payment_rate");
            }
        }

        private string _manager_name;
        public string manager_name 
        {
            get
            {
                return _manager_name;
            }
            set
            {
                _manager_name = value;
                NotifyPropertyChanged("manager_name");
            }
        }

        private double _manager_debate_rate;
        public double manager_debate_rate 
        {
            get
            {
                return _manager_debate_rate;
            }
            set
            {
                _manager_debate_rate = value;
                NotifyPropertyChanged("manager_debate_rate");
            }
        }

        private string _debate_card_code;
        public string debate_card_code
        {
            get
            {
                return _debate_card_code;
            }
            set
            {
                _debate_card_code = value;
                NotifyPropertyChanged("debate_card_code");
            }
        }

        private double _debate_card_amount;
        public double debate_card_amount
        {
            get
            {
                return _debate_card_amount;
            }
            set
            {
                _debate_card_amount = value;
                NotifyPropertyChanged("debate_card_amount");
            }
        }

        private string _contract_custom_name;
        public string contract_custom_name 
        {
            get
            {
                return _contract_custom_name;
            }
            set
            {
                _contract_custom_name = value;
                NotifyPropertyChanged("contract_custom_name");
            }
        }

        private string _contract_custom_contact;
        public string contract_custom_contact 
        {
            get
            {
                return _contract_custom_contact;
            }
            set
            {
                _contract_custom_contact = value;
                NotifyPropertyChanged("contract_custom_contact");
            }
        }

        private string _contract_discription;
        public string contract_discription 
        {
            get
            {
                return _contract_discription;
            }
            set
            {
                _contract_discription = value;
                NotifyPropertyChanged("contract_discription");
            }
        }

        private DateTime _contract_create_date;
        public DateTime contract_create_date
        {
            get
            {
                return _contract_create_date;
            }
            set
            {
                _contract_create_date = value;
                NotifyPropertyChanged("contract_create_date");
            }
        }

        private DateTime _contract_modify_date;
        public DateTime contract_modify_date
        {
            get
            {
                return _contract_modify_date;
            }
            set
            {
                _contract_modify_date = value;
                NotifyPropertyChanged("contract_modify_date");
            }
        }

        private int _apartment_id;
        public int apartment_id 
        {
            get
            {
                return _apartment_id;
            }
            set
            {
                _apartment_id = value;
                NotifyPropertyChanged("apartment_id");
            }
        }

        public string status_string
        {
            get
            {
                switch (contract_status)
                {
                    case 0:
                        return "Vaild";
                    case 1:
                        return "Invaild";
                }
                return "Unknown";
            }
            set
            {
                switch (value)
                {
                    case "Vaild":
                        contract_status = 0;
                        break;
                    case "Invaild":
                        contract_status = 1;
                        break;
                    default:
                        contract_status = 0;
                        break;
                }
            }
        }

        public double payment_rate
        {
            get
            {
                switch (contract_payment_terms)
                {
                    case 0:
                        return 0.98;
                    case 1:
                        return 0.99;
                }
                return 1;
            }
        }

        public string payment_string
        {
            get
            {
                switch (contract_payment_terms)
                {
                    case 0:
                        return "Lump Sum";
                    case 1:
                        return "Instalment";
                    case 2:
                        return "Mortgage Loan";
                }
                return "Unknown";
            }
            set
            {
                switch (value)
                {
                    case "Lump Sum":
                        contract_payment_terms = 0;
                        break;
                    case "Instalment":
                        contract_payment_terms = 1;
                        break;
                    case "Mortgage Loan":
                        contract_payment_terms = 2;
                        break;
                    default:
                        contract_payment_terms = 0;
                        break;
                }
            }
        }

        public string date_string
        {
            get
            {
                return contract_create_date.ToShortDateString();
            }
        }

        public bool has_changed { get; set; }

        public ContractView(DataRow r)
        {
            contract_id = Convert.ToInt32(r["contract_id"]);
            contract_code = Convert.ToString(r["contract_code"]).Replace("''","'");
            contract_status = Convert.ToInt32(r["contract_status"]);
            contract_debate_rate = Convert.ToDouble(r["contract_debate_rate"]);
            contract_payment_terms = Convert.ToInt32(r["contract_payment_terms"]);
            contract_payment_debate_rate = Convert.ToDouble(r["contract_payment_debate_rate"]);
            manager_name = Convert.ToString(r["manager_name"]).Replace("''", "'");
            manager_debate_rate = Convert.ToDouble(r["manager_debate_rate"]);
            debate_card_code = Convert.ToString(r["debate_card_code"]).Replace("''", "'");
            debate_card_amount = Convert.ToDouble(r["debate_card_amount"]);
            contract_custom_name = Convert.ToString(r["contract_custom_name"]).Replace("''", "'");
            contract_custom_contact = Convert.ToString(r["contract_custom_contact"]).Replace("''", "'");
            contract_discription = Convert.ToString(r["contract_discription"]).Replace("''", "'");
            contract_create_date = Convert.ToDateTime(r["contract_create_date"]);
            contract_modify_date = Convert.ToDateTime(r["contract_modify_date"]);
            apartment_id = Convert.ToInt32(r["apartment_id"]);

            Datarow = r;
            has_changed = false;
        }

        public ContractView()
        {
            contract_id = 0;
            contract_code = "";
            contract_status = 0;
            contract_debate_rate = 0.995;
            contract_payment_terms = 0;
            contract_payment_debate_rate = payment_rate;
            manager_name = "";
            manager_debate_rate = 1;
            debate_card_code = "";
            debate_card_amount = 0;
            contract_custom_name = "";
            contract_custom_contact = "";
            contract_discription = "";
            contract_create_date = DateTime.Now;
            contract_modify_date = DateTime.Now;
            apartment_id = 0;

            initDataRow();
            has_changed = false;
        }

        private DataRow _datarow;
        public DataRow Datarow
        {
            set
            {
                _datarow = value;
            }
            get
            {
                refreshDataRow();
                return _datarow;
            }
        }

        public string queryString
        {
            get
            {
                return "SELECT * FROM contract WHERE contract_id = " + contract_id;
            }
        }
        public string deleteString
        {
            get
            {
                return "DELETE FROM contract WHERE contract_id = " + contract_id;
            }
        }

        public readonly string tableName = "contract";
        public readonly int ColumnCount = 16;

        private void initDataRow()
        {
            string errMessage = "";
            DataTable table = AccdbConn.query(queryString, ref errMessage);
            Datarow = table.NewRow();
        }

        private void refreshDataRow()
        {
            DataRow r = _datarow;
            r["contract_id"] = contract_id;
            r["contract_code"] = contract_code.Replace("'", "''");
            r["contract_status"] = contract_status;
            r["contract_debate_rate"] = contract_debate_rate;
            r["contract_payment_terms"] = contract_payment_terms;
            r["contract_payment_debate_rate"] = contract_payment_debate_rate;
            r["manager_name"] = manager_name.Replace("'", "''");
            r["manager_debate_rate"] = manager_debate_rate;
            r["debate_card_code"] = debate_card_code.Replace("'", "''");
            r["debate_card_amount"] = debate_card_amount;
            r["contract_custom_name"] = contract_custom_name.Replace("'", "''");
            r["contract_custom_contact"] = contract_custom_contact.Replace("'", "''");
            r["contract_discription"] = contract_discription.Replace("'", "''");
            r["contract_create_date"] = contract_create_date;
            r["contract_modify_date"] = contract_modify_date;
            r["apartment_id"] = apartment_id;
        }

        public string Insert()
        {
            string errMessage = "";

            if (!AccdbConn.insert(queryString, tableName, ColumnCount, Datarow, ref errMessage))
            {
                errMessage = "Add the contract failed.\n" + errMessage;
                has_changed = true;
            }
            else
            {
                has_changed = false;
            }

            return errMessage;
        }

        public string Update()
        {
            string errMessage = "";

            if (!AccdbConn.update(queryString, tableName, ColumnCount, Datarow, ref errMessage))
            {
                errMessage = "Modify the contract failed.\n" + errMessage;
                has_changed = true;
            }
            else
            {
                has_changed = false;
            }

            return errMessage;
        }

        public string Delete()
        {
            string errMessage = "";
            AccdbConn.exec(deleteString, ref errMessage);

            if (errMessage != "")
                errMessage = "Remove the contract failed.\n" + errMessage;

            return errMessage;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                has_changed = true;
            }
        }

        public object Clone()
        {
            return new ContractView(Datarow);
        }
    }
}
